<?php
/**
 * Zend Framework Examples
 *
 * @category  Sfs
 * @package   TryIt
 * @author    Michel Corne
 * @copyright 2009 Michel Corne
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @see       http://zend-framework-examples.blogspot.com
 */

/*
 Handling locations
 */
class Sfs_Location
{
    /* Getting the latitude and longitude of a city */
    public function getDateCity($city, $timezone, $useTz, $horizon)
    {
        $city = $this->_findCity($city, $timezone, $useTz);
        $dateCity = Zend_Date_Cities::City($city, $horizon);

        $latitude= round($dateCity['latitude'], 2);
        $longitude= round($dateCity['longitude'], 2);

        return array($dateCity, $city, $latitude, $longitude);
    }

    /* Setting a location from the latitude and longitude */
    public function setDateCity($latitude, $longitude, $horizon)
    {
        $dateCity['latitude'] = $latitude;
        $dateCity['longitude'] = $longitude;
        $dateCity['horizon'] = $horizon;

        return $dateCity;
    }

    /* Finding a city in a timezone */
    private function _findCity($city, $timezone, $useTz)
    {
        // We get the list of available cities.
        $cities = Zend_Date_Cities::getCityList();

        if ($useTz) {
            // We extract the name of the city from the timezone if requested.
            $tzCity = Sfs_Timezone::getCity($timezone);
            // We verify if the city is valid,
            // otherwise we use the name of the city that was given.
            in_array($tzCity, $cities) and $found = $tzCity;
        }

        // We verify that the name of the city is valid.
        isset($found) or in_array($city, $cities) and $found = $city;

        if (empty($found)) {
            // We return an error if no city was identified.
            throw new Exception("No city could be identified!");
        }

        return $found;
    }
}
